package com.jdk.mode.relate;

/**
 * @author jukg
 * @date 2023/08/30 10:45
 * @description m命令模式  请求封装， 请求和执行请求 分开
 */
public class Command {

    public static void main(String[] args) {
        Light r = new LightImpl("RedLight");
        Light g = new LightImpl("GreenLight");

        LightPanel panel = new LightPanel(r);
        panel.on();
        panel.off();
        panel.setLight(g);
        panel.on();
        panel.off();
    }
}

interface  Light{
    void on();
    void off();
}

class LightPanel{

    private Light light;

    public LightPanel(Light light) {
        this.light = light;
    }

    public void setLight(Light light) {
        this.light = light;
    }

    public void on(){
        light.on();
    }
    public void off(){
        light.off();
    }
}


class LightImpl implements Light{
    private String name;
    public LightImpl(String name) {
        this.name= name;
    }

    @Override
    public void on() {
        System.out.println(name + "on");
    }

    @Override
    public void off() {
        System.out.println(name + "off");
    }
}
